package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.common.exception.CustException;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.admin.dtos.ChannelDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {
    @Autowired
    private AdChannelMapper adChannelMapper;

    //分页查询频道
    @Override
    public ResponseResult findByNameAndPage(ChannelDTO dto) {
        //非空判断
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "没有参数!");
        }
        //检查分页
//        dto.checkParam();
        //创建分页对象
        Page<AdChannel> page = new Page<>(dto.getPage(), dto.getSize());
        //查询条件
        LambdaQueryWrapper<AdChannel> wrapper = Wrappers.lambdaQuery();
        if (dto.getName() != null) {
            wrapper.like(AdChannel::getName, dto.getName());
        }
        if (dto.getStatus() != null) {
            wrapper.eq(AdChannel::getStatus, dto.getStatus());
        }
        wrapper.orderByAsc(AdChannel::getOrd);
        //分页查询
        adChannelMapper.selectPage(page, wrapper);
        //创建PageResponseResult对象
        PageResponseResult pageResult = new PageResponseResult();
        //封装数据
        pageResult.setCurrentPage(dto.getPage());
        pageResult.setData(page.getRecords());
        pageResult.setTotal(page.getTotal());
        pageResult.setSize(dto.getSize());
        //返回结果
        return pageResult;
    }

    //频道新增
    @Override
    public ResponseResult insert(AdChannel adChannel) {
        //非空判断
        if (adChannel == null || StringUtils.isBlank(adChannel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断新增频道名称的长度是否大于10
        if (adChannel.getName().length() > 10) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称的长度不能超过10!");
        }
        //根据新增频道的名称查询频道数量,如果>0,则报错
        Integer count = adChannelMapper.selectCount(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, adChannel.getName()));
        //设置查询条件
//        wrapper.eq(AdChannel::getName, adChannel.getName());
//        //查询
//        Integer count = adChannelMapper.selectCount(wrapper);
        //非空判断
        if (count > 0) {
            //有数据,报错
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //添加创建时间
        adChannel.setCreatedTime(new Date());
        //插入并判断
        if (adChannelMapper.insert(adChannel) != 1) {
            //操作失败
            CustException.cust(AppHttpCodeEnum.SERVER_ERROR);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    //频道修改&频道有效无效设置
    @Override
    public ResponseResult update(AdChannel adChannel) {
        //非空判断
        if (adChannel == null || adChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据频道的id查询频道
        AdChannel adChannel1 = adChannelMapper.selectById(adChannel.getId());
        //判断根据id是否查询到频道
        if (adChannel1 == null) {
            //没有查询到频道,报错
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //判断name是否修改
        if (!adChannel.getName().equals(adChannel1.getName())) {
            //修改了,根据name查询数据
            Integer count = adChannelMapper.selectCount(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, adChannel.getName()));
            //判断查询到的数据是否存在
            if (count > 0) {
                //该name已经存在
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
            }
            //判断name长度是否超过10
            if (adChannel.getName().length() > 10) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            }
        }
        //没有修改,直接执行修改方法并返回结果
        if (adChannelMapper.updateById(adChannel) != 1) {
            //操作失败
            CustException.cust(AppHttpCodeEnum.SERVER_ERROR);
        }
        //修改成功,给出成功提示
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    //频道删除
    @Override
    public ResponseResult deleteById(Integer id) {
//        int i = 1 / 0;
        //非空判断
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据id查询频道
        AdChannel adChannel = adChannelMapper.selectById(id);
        //判断频道是否存在
        if (adChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMING_CHANNEL_NOT_EXIST);
        }
        //判断频道状态
        if (adChannel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMING_CHANNEL_STATUS_TRUE);
        }
        //执行删除操作
        if (adChannelMapper.deleteById(id) != 1) {
            //删除失败
            CustException.cust(AppHttpCodeEnum.SERVER_ERROR);
        }
        //删除成功
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
